package com.gexiaobao.pigeon.viewmodel

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.DailyScanResponse
import com.gexiaobao.pigeon.app.model.bean.GPSRingInfoResponse
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.model.bean.TrainingInfoResponse
import com.gexiaobao.pigeon.app.model.bean.TrainingListResponse
import com.gexiaobao.pigeon.app.model.bean.TrainingResultResponse
import com.gexiaobao.pigeon.app.model.bean.TrainingSinglePigeonResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.CreateTrainingParams
import com.gexiaobao.pigeon.app.model.param.StartTrainingParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.RequestBody

/**
 * @author:hxw
 * @date  :2022/12/30
 * @desc  :
 */
class MyTrainingViewModel : BaseViewModel() {

    private var pageSize = 20

    val searchName = StringObservableField()

    var pigeonData: MutableLiveData<ListDataUiState<PigeonBean.RingInfo>> = MutableLiveData()

    /** 添加训放 */
    val addTraining: MutableLiveData<UiState> = MutableLiveData()

    /** 删除训放 */
    val deleteTraining: MutableLiveData<UiState> = MutableLiveData()

    /** 结束训放 */
    val stopTraining: MutableLiveData<UiState> = MutableLiveData()

    /** 开始训放 */
    val startTrainingResult: MutableLiveData<UiState> = MutableLiveData()

    /** 添加单只训放训放时间 */
    val updateTrainingTimeResult: MutableLiveData<UiState> = MutableLiveData()

    /** 我的训放列表 */
    val trainingListData: MutableLiveData<ListDataUiState<TrainingListResponse.InfoList>> = MutableLiveData()

    /** 获取训放信息 */
    var trainingInfo = MutableLiveData<ResultState<TrainingInfoResponse>>()

    /** 获取训放结果 */
    var trainingResult: MutableLiveData<ListDataUiState<TrainingResultResponse.ResultList>> = MutableLiveData()

    /** 获取单只训放赛鸽列表 */
    var trainingSinglePigeonData: MutableLiveData<ListDataUiState<TrainingSinglePigeonResponse.SinglePigeonList>> = MutableLiveData()

    /** 获取训放信息 */
    var training = MutableLiveData<ResultState<TrainingInfoResponse>>()

    /** 训放详情基本信息*/
    //放飞时间
    var trainingTime = StringObservableField()

    //放飞地点
    var trainingAddress = StringObservableField()

    //放飞经度
    var trainingLon = StringObservableField()

    //放飞纬度
    var trainingLat = StringObservableField()

    //鸽舍经度
    var pigeonHouseLon = StringObservableField()

    //割舍纬度
    var pigeonHouseLat = StringObservableField()

    //放飞空距
    var trainingDistance = StringObservableField()

    //放飞天气
    var trainingWeather = StringObservableField()

    //参考风向
    var trainingWindDirection = StringObservableField()

    //参考风力
    var trainingWindPower = StringObservableField()

    //放飞羽数
    var trainingPigeonNo = StringObservableField()

    //归巢羽数
    var trainingGuiPigeonNo = StringObservableField()

    //训放按钮文字类型
    var trainingType = StringObservableField()

    //底部左侧文字类型
    var leftTrainingType = StringObservableField()

    //底部右侧文字类型
    var rightTrainingType = StringObservableField()

    var isShowSingleTraining = BooleanObservableField()
    var isShowShare = BooleanObservableField()

    //每日扫描-选择日期
    var pickDateText = StringObservableField()

    /** 训放详情中是否显示单只训放按钮 */
    var showRightMenuSingle = object : ObservableInt(isShowSingleTraining) {
        override fun get(): Int {
            return if (isShowSingleTraining.get()) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    /** 训放详情中是否显示分享按钮 */
    var showRightMenuShare = object : ObservableInt(isShowShare) {
        override fun get(): Int {
            return if (isShowShare.get()) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    /** 获取我的赛鸽列表列表 */
    fun getPigeonList(isRefresh: Boolean, type: String) {
        val body = hashMapOf<String, Any>().apply {
            put("type", type)
        }
        request({ apiService.getPigeonList(body) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = false,
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.ringInfo
            )
            pigeonData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = isRefresh, listData = arrayListOf<PigeonBean.RingInfo>()
            )
            pigeonData.value = listDataUiState
        })
    }

    /** 获取训放gps轨迹 */
    val ringInfosResult: MutableLiveData<GPSRingInfoResponse> = MutableLiveData()
    fun getGpsRingInfosForTraining(body: RequestBody) {
        request({ apiService.getGpsRingInfosForTraining(body) }, {
            ringInfosResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 创建训放 */
    fun createTraining(paramsBody: CreateTrainingParams) {
        request({ apiService.createTraining(paramsBody) }, {
            val deleteState = UiState(isSuccess = true, id = it.id)
            addTraining.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            addTraining.value = deleteState
        })
    }

    /** 获取训放列表 */
    fun getTrainingList() {
        request({ apiService.getTrainingList() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), hasMore = false, isFirstEmpty = it.isEmpty(), listData = it.info
            )
            trainingListData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = true, listData = arrayListOf<TrainingListResponse.InfoList>()
            )
            trainingListData.value = listDataUiState
        })
    }

    /** 获取训放结果 */
    fun getTrainingResult(id: String, name: String) {
        request({ apiService.getTrainingResult(id, name) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), hasMore = false, isFirstEmpty = it.isEmpty(), listData = it.list
            )
            trainingResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = true, listData = arrayListOf<TrainingResultResponse.ResultList>()
            )
            trainingResult.value = listDataUiState
        })
    }

    /** 获取单只训放赛鸽列表 */
    fun getTrainingSinglePigeon(id: String) {
        request({ apiService.getTrainingSinglePigeon(id) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), hasMore = false, isFirstEmpty = it.isEmpty(), listData = it.list
            )
            trainingSinglePigeonData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = true, listData = arrayListOf<TrainingSinglePigeonResponse.SinglePigeonList>()
            )
            trainingSinglePigeonData.value = listDataUiState
        })
    }

    /** 删除我的训放 */
    fun deleteTraining(id: String) {
        request({ apiService.deleteTraining(id) }, {
            val deleteState = UiState(isSuccess = true)
            deleteTraining.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            deleteTraining.value = deleteState
        })
    }

    /** 结束我的训放 */
    fun stopTraining(id: String) {
        request({ apiService.stopTraining(id) }, {
            val deleteState = UiState(isSuccess = true)
            stopTraining.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            stopTraining.value = deleteState
        })
    }

    fun startTraining(param: StartTrainingParams) {
        request({ apiService.startTraining(param) }, {
            val deleteState = UiState(isSuccess = true)
            startTrainingResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            startTrainingResult.value = deleteState
        })
    }

    fun updateTrainingPigeon(id: String) {
        request({ apiService.updateTrainingPigeon(id) }, {
            val updateState = UiState(isSuccess = true)
            updateTrainingTimeResult.value = updateState
        }, {
            val updateState = UiState(isSuccess = false, errorMsg = it.msg)
            updateTrainingTimeResult.value = updateState
        })
    }

    /** 获取训放信息 */
    fun getTrainingInfo(id: String) {
        request(
            { apiService.getTrainingInfo(id) },
            //请求成功 已自动处理了 请求结果是否正常
            trainingInfo,
            true,
            appContext.getString(R.string.loading)
        )
    }

    /** 获取训放信息 */
    fun getTraining(id: String) {
        request(
            { apiService.getTraining(id) },
            //请求成功 已自动处理了 请求结果是否正常
            training,
            true,
            appContext.getString(R.string.loading)
        )
    }

    /** 重置每日扫描 */
    var cleanDailyScanResult: MutableLiveData<UiState> = MutableLiveData()
    fun clearDailyScan() {
        request(
            { apiService.clearDailyScan() }, {
                val cleanState = UiState(isSuccess = true)
                cleanDailyScanResult.value = cleanState
            }, {
                val cleanState = UiState(isSuccess = false, errorMsg = it.msg)
                cleanDailyScanResult.value = cleanState
            }
        )
    }

    /** 每日扫描结果 */
    var dailyScanListResult: MutableLiveData<ListDataUiState<DailyScanResponse.DataList>> = MutableLiveData()
    fun getScanDetail(isRefresh: Boolean, ringSn: String, fosterName: String) {
        if (isRefresh) {
            pageSize = 20
        }
        request({ apiService.getScanDetail(ringSn, 1, pageSize, fosterName) }, {
            pageSize += 20
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = isRefresh, isEmpty = it.isEmpty(), isFirstEmpty = isRefresh && it.isEmpty(), listData = it.list
            )
            dailyScanListResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<DailyScanResponse.DataList>()
            )
            dailyScanListResult.value = listDataUiState
        })
    }

}